﻿using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Relocation.Base;
using Relocation.Base.UI;
using Relocation.Com;
using Relocation.Data;
using Relocation.UI.Interface;


namespace Relocation.UI
{
	public partial class statistics : BaseForm, IPrintForm
	{

		//拆迁户
		private int countHousesAll = 0;         //所有拆迁户的个数
		private int countHousesArranged = 0;    //已经安置的个数
		private int countHousesArranging = 0;   //正在安置的个数
		private int countHousesNoArrange = 0;   //没有安置的个数
		//安置人员
		private int countPersonAll = 0;         //安置人员总人数
		private int countPersonArranged = 0;    //已经安置的个数        
		private int countPersonNoArrange = 0;   //没有安置的个数
		//金额
		private decimal total2 = 0;
		//安置房信息
		private int[] HouseCountWant;    //意向安置房数
		private int[] HouseCountarranged;//已经安置房数


		public statistics()
			: base()
		{
			InitializeComponent();
			setProject();
		}
		public statistics(Session _session)
			: base(_session)
		{
			InitializeComponent();
			setProject();
		}


		/// <summary>
		/// 显示结果
		/// </summary>
		private void setValue()
		{
			try
			{
				this.textHousesAll.Text = countHousesAll.ToString() + "户";    //已经安置的个数
				this.textHousesArranged.Text = countHousesArranged.ToString() + "户";    //已经安置的个数
				this.textHousesArranging.Text = countHousesArranging.ToString() + "户";   //正在安置的个数
				this.textHousesNoArrange.Text = countHousesNoArrange.ToString() + "户";   //没有安置的个数
				//安置人员
				this.textPersonsAll.Text = countPersonAll.ToString() + "人";         //安置人员总人数
				this.textPersonsArranged.Text = countPersonArranged.ToString() + "人";    //已经安置的个数        
				this.textPersonsNoArrange.Text = (countPersonAll - countPersonArranged).ToString() + "人";   //没有安置的个数  

				System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("zh-CN");
				this.textSum.Text = total2.ToString("N02", ci) + "元";

				dataGridView1.Rows.Clear();
				string[] housesType = new string[] { "50平米户型", "70平米户型", "90平米户型", "110平米户型", "130平米户型", "储藏室", "门市", "合计：" };
				for (int i = 0; i < housesType.Count(); i++)
				{
					dataGridView1.Rows.Add(new string[] { housesType[i], HouseCountWant[i].ToString(), HouseCountarranged[i].ToString(), (HouseCountWant[i] - HouseCountarranged[i]).ToString() });
				}
				this.Enabled = true;
				this.UseWaitCursor = false;
			}
			catch (Exception ex)
			{
				Log.Error(ex);				
				throw;
			}
		}

		/// <summary>
		///  清除结果
		/// </summary>
		private void setDefault()
		{
			countHousesAll = 0;         //所有拆迁户的个数
			countHousesArranged = 0;    //已经安置的个数
			countHousesArranging = 0;   //正在安置的个数
			countHousesNoArrange = 0;   //没有安置的个数
			//安置人员
			countPersonAll = 0;         //安置人员总人数
			countPersonArranged = 0;    //已经安置的个数        
			//private int countPersonNoArrange = 0;   //没有安置的个数
			//金额
			total2 = 0;
			HouseCountWant = null;
			HouseCountarranged = null;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			Project project = comboBoxProject.SelectedItem as Project;
			if (project == null)
				return;
			DateTime? timeFrom = this.dateTimePickerFrom.Value;
			DateTime? timeTo = this.dateTimePickerTo.Value;
			
			ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object obj)
			{
				try
				{
					IQueryable<Relocatee> relocatees = GetRelicateesByDateTime(project, timeFrom, timeTo);
					this.Statistics(project, relocatees);
					this.ExecMethodInvoker(new MethodInvoker(delegate()
					{
						this.setValue();
					}));

				}
				catch (Exception ex)
				{
					Log.Error(ex);					
					this.ExecMethodInvoker(new MethodInvoker(delegate()
					{
						MyMessagebox.Error("统计失败：" + ex.Message);
						this.Enabled = true;
						this.UseWaitCursor = false;
					}));
				}
			}));
			this.Enabled = false;
			this.UseWaitCursor = true;
		}


		private IQueryable<Relocatee> GetRelicateesByDateTime(Project project, DateTime? dateTimeFrom, DateTime? dateTimeTo)
		{
			IQueryable<Relocatee> query = project.Relocatees.CreateSourceQuery().AsQueryable();
			if (dateTimeTo.HasValue || dateTimeFrom.HasValue)
				query =query.Where(t=>t.enabledate.HasValue);
			 if (dateTimeTo.HasValue)			
				query = query.Where(t => t.enabledate.Value <= dateTimeTo.Value);			
			else if (dateTimeFrom.HasValue)			
				query = query.Where(t => t.enabledate.Value >= dateTimeFrom.Value);			
			 return query;
		}

		private void Statistics(Project project, IQueryable<Relocatee> relocatees)
		{
			//拆迁户
			countHousesAll = relocatees.Count();         //所有拆迁户的个数
			countHousesArranged = relocatees.Count(t => t.status == 2);    //已经安置的个数
			countHousesArranging = relocatees.Count(t => t.status == 1);   //正在安置的个数
			countHousesNoArrange = relocatees.Count(t => t.status == 0);   //没有安置的个数

			//安置人员		
			IQueryable<Placement> placments = this.Session.DataModel.Placements.Where(t => relocatees.Select(t2 => t2.relocatees_id).Contains(t.Relocatee.relocatees_id));
			IQueryable<Relocatees_House> relocateHouse = this.Session.DataModel.Relocatees_House.Where(t => relocatees.Select(t2 => t2.relocatees_id).Contains(t.Relocatee.relocatees_id));
			countPersonAll = placments.Count();         //安置人员总人数			
			countPersonArranged = Convert.ToInt32(relocateHouse.Sum(t => t.persons).GetValueOrDefault(0));    //已经安置的个数        
			countPersonNoArrange = countPersonAll - countPersonArranged;   //没有安置的个数

			//金额
			total2 = relocatees.Sum(t => t.Relocatees_House.Sum(t1 => t1.money1 + t1.money2 + t1.money3 + t1.money4 + t1.money5).Value - (t.payment.HasValue ? t.payment.Value : 0));


			HouseCountWant = new int[8];
			HouseCountWant[0] = relocatees.Sum(t => t.housetype5).GetValueOrDefault(0);
			HouseCountWant[1] = relocatees.Sum(t => t.housetype1).GetValueOrDefault(0);
			HouseCountWant[2] = relocatees.Sum(t => t.housetype2).GetValueOrDefault(0);
			HouseCountWant[3] = relocatees.Sum(t => t.housetype3).GetValueOrDefault(0);
			HouseCountWant[4] = relocatees.Sum(t => t.housetype4).GetValueOrDefault(0);
			HouseCountWant[5] = relocatees.Sum(t => t.housetype6).GetValueOrDefault(0);
			HouseCountWant[6] = relocatees.Sum(t => t.housetype7).GetValueOrDefault(0);
			HouseCountWant[7] = HouseCountWant.Sum();

			HouseCountarranged = new int[8];
			HouseCountarranged[0] = relocateHouse.Where(t => t.House.areatype == 4).Count();
			HouseCountarranged[1] = relocateHouse.Where(t => t.House.areatype == 0).Count();
			HouseCountarranged[2] = relocateHouse.Where(t => t.House.areatype == 1).Count();
			HouseCountarranged[3] = relocateHouse.Where(t => t.House.areatype == 2).Count();
			HouseCountarranged[4] = relocateHouse.Where(t => t.House.areatype == 3).Count();
			HouseCountarranged[5] = relocateHouse.Where(t => t.House.areatype == 5).Count();
			HouseCountarranged[6] = relocateHouse.Where(t => t.House.areatype == 6).Count();
			HouseCountarranged[7] = HouseCountarranged.Sum();

		}

		/// <summary>
		/// 显示进度条,并设置最大值
		/// </summary>
		private void progressBarShow(int countHousesAll)
		{
			this.ExecMethodInvoker(new MethodInvoker(delegate()
			{
				progressBar1.Visible = true;
				progressBar1.Value = 0;
				progressBar1.Maximum = countHousesAll;  //设置进度条总数            
			}));
		}

		private void progressBarPerformStep()
		{
			this.ExecMethodInvoker(new MethodInvoker(delegate()
			{
				progressBar1.PerformStep(); //加载进度条       
			}));
		}

		private void setProject()
		{
			foreach (Project p in this.Session.DataModel.Projects)
				this.comboBoxProject.Items.Add(p);
			this.comboBoxProject.ValueMember = "project_id";
			this.comboBoxProject.DisplayMember = "name";
			this.comboBoxProject.DataSource = this.comboBoxProject.Items;
			this.comboBoxProject.SelectedValue = this.Session.Project.project_id;
		}


		#region IPrintForm 成员

		public void Print()
		{
			Relocation.Com.Tools.ExcelHelp.DataGridToExcel(this.dataGridView1);
			// GridToExcel.print(this.dataGridView1);
		}

		#endregion
	}
}
